﻿
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Text.Json.Nodes;
using LitJson;
using Sunny.UI;



namespace XMerp
{



    public class Controls_Form
    {
        //窗口
        public static View_MainForm? _form_Main = Controller.view_MainForm;

        //模块
        public static Module_WaitPrint? _module_WaitPrint = new(); //1.待打印订单页面
        public static View_Setting? _module_Setting { get; set; }//3.设置页面
        public static View_CheckProducts? _module_CheckOrder = new();//2.验货页面

        public static int currentModule = 0;
        //数据库
        public static OrderList orderList = new();




        public class OrderList
        {
            public ConcurrentQueue<Order> queue = new();
            public List<Order> orders = new();
            public Dictionary<string, List<Order>> mergeFlagOrders = new();
            public Dictionary<string, Order> snOrders = new();
            public List<Order>? selectList = new();
            public List<Order>? searchByOrderSn = new();
            public void ClearQueue()
            {
                while (queue.TryDequeue(out Order o))
                {
                    AddOrUpData(o);
                }
            }

            public void ClearQueueByOrderSn()
            {
                searchByOrderSn.Clear();
                while (queue.TryDequeue(out Order o))
                {
                    searchByOrderSn.Add(o);
                }
            }

            private void AddOrUpData(Order o)
            {
                if (snOrders.TryGetValue(o._orderSn, out Order _order))
                {
                    //订单存在 更新状态
                    if (_order._receiveName != o._receiveName) _order._receiveName = o._receiveName;

                    if (_order._receiveMobile != o._receiveMobile) _order._receiveMobile = o._receiveMobile;

                    if (_order._remarks != o._remarks) _order._remarks = o._remarks;

                    if (_order._receiveCity != o._receiveCity) _order._receiveCity = o._receiveCity;

                    if (_order._receiveAddress != o._receiveAddress) _order._receiveAddress = o._receiveAddress;

                    if (_order.订单状态 != o.订单状态) _order.订单状态 = o.订单状态;
                }
                else
                {
                    o.订单状态 = "waitPrint";
                    if (!string.IsNullOrEmpty(o.preMergeFlag))
                    {
                        if (mergeFlagOrders.TryGetValue(o.preMergeFlag, out List<Order> _list))
                        {
                            //关系库存在合并订单
                            o.canMerged = true;
                            _list.Add(o);
                        }
                        else
                        {
                            //无合并订单
                            var list = orders.FindAll(p => p.preMergeFlag == o.preMergeFlag);
                            if (list.Count > 0)
                            {
                                //存在可以合并订单
                                foreach (var item in list)
                                {
                                    item.canMerged = true;
                                }
                                o.canMerged = true;
                                list.Add(o);
                                mergeFlagOrders.Add(o.preMergeFlag, list);
                            }
                        }
                    }
                    orders.Add(o);
                    snOrders.Add(o._orderSn, o);
                }
            }

            public List<Order> GetWaitPrintOrders()
            {
                return orders.FindAll(o => o.订单状态 == "waitPrint");
            }

            public string GetWaitPrintCount()
            {
                return orders.FindAll(o => o.订单状态 == "waitPrint").Count.ToString();
            }
        }
        public class ShopInfo
        {
            public string mallName { get; set; }
            public string mallCode { get; set; }
            public long mallId { get; set; }
            public long shopsEndTime { get; set; }
            public string mallPlatformCode { get; set; }
            public string platformName { get; set; }
        }
        public class Order
        {
            //客户管家订单的id
            public string id { get; set; }
            //合单关键字
            public string? preMergeFlag { get; set; }

            public bool isMerged { get; set; }
            public bool isLocked { get; set; }
            public bool canMerged { get; set; }
            public string? 订单状态 { get; set; }
            public string? orderStatus { get; set; }
            public string? expressName { get; set; }
            public string? expressCode { get; set; }
            public string? expressNo { get; set; }
            public List<string>? expressNos = new();
            public int checkCount { get; set; }
            public List<string>? checkTime { get; set; }
            public Module_WaitPrient_OrderInfo? M_WP_OI { get; set; }



            //店铺编号
            public string? _mallCode { get; set; }
            //电商平台订单的编号
            public string? _orderSn { get; set; }
            //最晚发货时间
            public string? _lastShipTime { get; set; }
            //收件人
            public string? _receiveName { get; set; }
            //电话
            public string? _receiveMobile { get; set; }
            //省市区
            public string? _receiveCity { get; set; }
            //详细地址
            public string? _receiveAddress { get; set; }
            //订单备注
            public string? _remarks { get; set; }
            public List<Products>? _list_Products { get; set; }

        }
        public class Products
        {   //客户管家product的id
            public string? id { get; set; }

            public string? _title { get; set; }
            public string? _productId { get; set; }
            public string? _skuName { get; set; }
            public string? _skuId { get; set; }
            public string? _skuImg { get; set; }
            public int _skuCount { get; set; }
        }
    }
}
